﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using Newtonsoft.Json;
using NHibernate.Transform;
using System.Collections;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.CustomQuery
{
    public partial class QueryDesplay : System.Web.UI.Page
    {
        protected class PropertyItem
        {
            public string Code { get; set; }
            public string Text { get; set; }
            public string Format { get; set; }
            public string Value{get;set;}
            public string Memo { get; set; }
            public string PropType{get;set;}
            public string CompaireType{get;set;}
        }

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected IList<PropertyItem> propList { get; set;}

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                SysCustomQuery customQuery = session.Get<SysCustomQuery>(2);

                IList<PropertyItem> paramList = JsonConvert.DeserializeObject<IList<PropertyItem>>(customQuery.ParamList);
                queryRepeater.DataSource = paramList;
                queryRepeater.DataBind();

                QueryFilterList filters = new QueryFilterList();
                foreach (PropertyItem item in paramList)
                {
                    if (string.IsNullOrEmpty(Request[item.Code]))
                    {
                        continue;
                    }
                    string[] datas = item.Code.Split(new string[] { "_SP_" }, StringSplitOptions.RemoveEmptyEntries);
                    if (datas.Length != 2)
                    {
                        continue;
                    }
                    string format = string.IsNullOrEmpty(item.Format) ? "{0}":item.Format;
                    string value = string.Format(format, Request[item.Code]);
                    if (item.PropType == "string")
                    {
                        filters.Add(new StringFilter(datas[0], datas[1], value, (StringCompairType)Enum.Parse(typeof(StringCompairType), item.CompaireType)));
                    }
                    else
                    {
                        filters.Add(new NumberFilter(datas[0], datas[1], value, (NumberCompairType)Enum.Parse(typeof(NumberCompairType), item.CompaireType)));
                    }

                }
                propList = JsonConvert.DeserializeObject<IList<PropertyItem>>(customQuery.ResultList);
                propRepeater.DataSource = propList;
                propRepeater.DataBind();

                string sql = customQuery.Sql;
                foreach (DataFilter item in filters)
                {
                    sql += " and " + item.GetSection();
                }
                IQuery query = session.CreateSQLQuery(sql);
                filters.SetUp(query);

                dataRepeater.DataSource = query.SetResultTransformer(Transformers.AliasToEntityMap).List();
                dataRepeater.DataBind();
                ////设置记录总数
                //_framePage.RecordCount = Convert.ToInt32(session.CreateQuery("select count(*) from SysCustomQuery").UniqueResult());

                ////设置数据源
                //repeater1.DataSource = session.CreateQuery("from SysCustomQuery")
                //    .SetFirstResult((pageIndex - 1) * pageSize)
                //    .SetMaxResults(pageSize)
                //    .List<SysCustomQuery>();
            }


        }

        protected void repeaterObjects_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IDictionary item = e.Item.DataItem as IDictionary;
                foreach (PropertyItem prop in propList)
                {
                    string format = string.IsNullOrEmpty(prop.Format) ? "{0}":prop.Format;
                    prop.Value = string.Format(format, item[prop.Code]);
                }
                Repeater rep = e.Item.FindControl("itemRepeater") as Repeater;//找到里层的repeater对象

                rep.DataSource = propList;
                rep.DataBind();
            }
        }
    }
}